﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Du_Lich
{
    public partial class ThongTinKhachHangForm : Form
    {
        public ThongTinKhachHangForm()
        {
            InitializeComponent();
        }

        string strConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString;
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataAdapter da = null;
        DataTable dt = null;



        private void ThongTinKhachHangForm_Load(object sender, EventArgs e)
        {
            MoKetNoi();
            gvKhachHang.DataSource = ThongTinKH();

            txtMaNV.Text = DangNhapForm.manv.ToString();
        }

        private void ThongTinKhachHangForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DongKetNoi();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemKH(txtMaKH.Text, DateTimeNgayLap.Value.ToString(), txtHoTen.Text, DateTimeNgaySinh.Value.ToString(), txtDiaChi.Text, cbGioiTinh.Text, txtCMND.Text, txtghichu.Text);
            gvKhachHang.DataSource = ThongTinKH();
        }

        public void ThemKH(string makh, string ngaylap, string hotenkh, string ngaysinh, string diachi, string gioitinh, string cmnd, string ghichu)
        {
            string strInsert = "Insert KHACHHANG values(@MaKh,@NgayLap,@HoTenKH,@NgaySinh,@diachi,@gioitinh,@cmnd,@ghichu)";
            cmd = new SqlCommand(strInsert, conn);
            cmd.Parameters.AddWithValue("@MaKH", makh);
            cmd.Parameters.AddWithValue("@NgayLap", ngaylap);
            cmd.Parameters.AddWithValue("@HoTenKH", hotenkh);
            cmd.Parameters.AddWithValue("@NgaySinh", ngaysinh);
            cmd.Parameters.AddWithValue("@diachi", diachi);
            cmd.Parameters.AddWithValue("@gioitinh", gioitinh);
            cmd.Parameters.AddWithValue("@cmnd", cmnd);
            cmd.Parameters.AddWithValue("@ghichu", ghichu);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void Sua(string makh,string ngaylap, string hotenkh, string ngaysinh, string diachi, string gioitinh, string cmnd, string ghichu)
        {
            string strUpdate = "Update KHACHHANG set NgayLap=@ngaylap,HoTenKH=@htkh,NgaySinh=@ns,DiaChi=@dc,GioiTinh=@gt,CMND=@cmnd,GhiChu=@gc where MaKH=@makh";
            cmd = new SqlCommand(strUpdate, conn);
            cmd.Parameters.AddWithValue("@ngaylap", ngaylap);
            cmd.Parameters.AddWithValue("@htkh", hotenkh);
            cmd.Parameters.AddWithValue("@ns", ngaysinh);
            cmd.Parameters.AddWithValue("@dc", diachi);
            cmd.Parameters.AddWithValue("@gt", gioitinh);
            cmd.Parameters.AddWithValue("@cmnd", cmnd);
            cmd.Parameters.AddWithValue("@gc", ghichu);
            cmd.Parameters.AddWithValue("@makh", makh);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void MoKetNoi()
        {
            conn = new SqlConnection(strConn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
        }

        public void DongKetNoi()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        public DataTable ThongTinKH()
        {
            string strSql = "Select * From KHACHHANG";
            da = new SqlDataAdapter(strSql, conn);
            dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public void Xoa(string makh)
        {
            string strDelete = "Delete KHACHHANG where makh=@makh";
            cmd = new SqlCommand(strDelete, conn);
            cmd.Parameters.AddWithValue("@makh", makh);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private int TimKiem()
        {
            string chuoitim = txtMaKH.Text;
            int i = -1;
            foreach (DataGridViewRow dgv in gvKhachHang.Rows)
            {
                try
                {
                    i++;
                    if (string.Compare(chuoitim, dgv.Cells[0].Value.ToString()) == 0)
                        return i;
                }
                catch (Exception)
                {
                    MessageBox.Show("Ban nhap sai roi ! Nhap lai di");
                }
            }
            return -1;
        }


        private void gvKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            if (gvKhachHang.SelectedRows.Count == 0)
                return;
            int i = gvKhachHang.SelectedRows[0].Index;
            txtMaKH.Text = gvKhachHang.Rows[i].Cells[0].Value.ToString();
            DateTimeNgayLap.Value = DateTime.Parse(gvKhachHang.Rows[i].Cells[1].Value.ToString());
            txtHoTen.Text = gvKhachHang.Rows[i].Cells[2].Value.ToString();
            DateTimeNgaySinh.Value = DateTime.Parse(gvKhachHang.Rows[i].Cells[3].Value.ToString());
            txtDiaChi.Text = gvKhachHang.Rows[i].Cells[4].Value.ToString();
            cbGioiTinh.Text = gvKhachHang.Rows[i].Cells[5].Value.ToString();
            txtCMND.Text = gvKhachHang.Rows[i].Cells[6].Value.ToString();
            txtghichu.Text = gvKhachHang.Rows[i].Cells[7].Value.ToString();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            int kq = TimKiem();
            if (kq == -1)
                MessageBox.Show("Khong tim thay", "Thong bao tim kiem");
            else
                gvKhachHang.Rows[kq].Selected = true;
        }

        private void btndattour_Click(object sender, EventArgs e)
        {
            try
            {
                QuanLyDatTourForm qldt = new QuanLyDatTourForm(gvKhachHang.SelectedRows[0].Cells[0].Value.ToString());
                qldt.ShowDialog();
            }
            catch
            {
                MessageBox.Show("bạn chưa chọn khách hàng");
            }

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvKhachHang.Rows.Count == 0)
                    return;
                int i = gvKhachHang.SelectedRows[0].Index;
                Xoa(gvKhachHang.Rows[i].Cells[0].Value.ToString());
                gvKhachHang.DataSource = ThongTinKH();
            }
            catch
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DongKetNoi();
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua(txtMaKH.Text, DateTimeNgayLap.Value.ToString("MM/dd/yyyy"), txtHoTen.Text, DateTimeNgaySinh.Value.ToString("MM/dd/yyyy"), txtDiaChi.Text, cbGioiTinh.Text, txtCMND.Text, txtghichu.Text);
            gvKhachHang.DataSource = ThongTinKH();
        }
    }
}
